<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Customer</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <script src="js/common.js" type="text/javascript"></script>
    <script src="js/jquery.js"></script>
    <script src="js/jsrender.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="text/javascript">
        $(function () {
            if(localStorage.getItem("token")==null){
                window.location.href="login.html"
            }
            //获取护士id
            var nid
            if(localStorage.getItem("position")==1){
                nid = 0
                list()
            }
            else{$.ajax({
                url:GC.bctx + "customers/getnid",//后端请求映射路径
                type:"GET",
                data:{useraccount:localStorage.getItem("account")},
                headers: {"Authorization": localStorage.getItem("token")},
                success:function (r) {
                    if(r.successed){
                        nid = r.data
                        list()
                    }else{
                        alert(r.message)
                    }
                }
            })
            }
            //请求所有客户信息(可查看的客户)
            function list(){
                $.ajax({
                    url:GC.bctx + "customers/list/"+nid,//后端请求映射路径
                    type:"GET",
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function (r) {
                        if(r.successed){
                            //jsrender模板渲染
                            $("#customers").html($("#customersTemplate").render(r.data))
                        }else{
                            window.location.href="login.html"
                        }
                    }
                })
            }
            
            //form提交组合查询
            $("form").submit(function(){
                //异步提交
                //数据验证交给后端数据检验
                //ajax
                $.ajax({
                    url:GC.bctx + "customers/find",
                    type:"GET",
                    data:$("form").serialize(),//需要配上属性name：name="id" name="name"
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //成功了渲染
                            $("#customers").html($("#customersTemplate").render(r.data))
                        }else{
                            alert(r.message)
                        }
                    }
                })
                //阻止表单正常提交
                return false;
            })
            //转到出入记录信息页面
            $("#customers").on("click",".btn-update",function () {
                var id = $(this).attr("data-id")
                $.ajax({
                    url:GC.bctx + "customers/detail/"+id,
                    type:"GET",
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //成功后
                            alert(r.message)
                            localStorage.setItem("customerid",id);//跳转前保存选中的客户信息
                            window.location.href="listGoout.html"
                        }else{
                            alert(r.message)
                        }
                    }
                })

            })
            //删除数据（确认离院，将flag改为0,修改入住登记表中对应数据）
            $("#customers").on("click",".btn-delete",function () {
                var id = $(this).attr("data-id")            //取值方法
                //删除2：二次确认
                if(!window.confirm("确定删除以下编号的客户吗？ \n" + id + "\n请确认：")){
                    return;
                }
                if(!window.confirm("再次确认")){
                    return;
                }
                //ajax提交请求
                $.ajax({
                    url:GC.bctx + "customers/left/"+id,
                    type:"PUT",
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //成功后
                            alert(r.message)
                            window.location.href="customers.html"
                        }else{
                            alert(r.message)
                        }
                    }
                })
            })
        })
    </script>
    <script id="customersTemplate" type="text/x-jsrender">
	<tr align="center">
		<td>{{:customerid}}</td>
		<td>{{:customername}}</td>
		<td>{{:customersex}}</td>
		<td>{{:customerage}}</td>
		<td>{{:customertelephone}}</td>
		<!--<td>{{:bedid}}</td>-->
		<td colspan="2" align="center">
            <button name="goInfo" class="btn btn-update btn-info" data-id="{{:customerid}}">出入信息</button>
            <button name="deleteCus" class="btn btn-delete btn-danger" data-id="{{:customerid}}">登记离院</button>
         </td>
	</tr>
    </script>
</head>
<body>
<form  class="container-fluid">
    <label>客户id：<input name="customerid" type="text"/></label>
    <label>客户姓名：<input name="customername" type="text"/></label>
    <input type="submit" value="查 询"/>
    <input type="reset" value="重 置"/>
</form>
<table class="table table-bordered">
    <tr>
        <th>客户id</th>
        <th>客户姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>联系电话</th>
        <th> </th>
    </tr>
    <tbody id="customers"></tbody>
</table>
</body>
</html>